package com.pig4cloud.pig.village.appcollect.controller;

import cn.hutool.core.bean.BeanUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.village.api.app.AppCollectDto;
import com.pig4cloud.pig.village.api.app.AppEventDto;
import com.pig4cloud.pig.village.api.app.AppMainDto;
import com.pig4cloud.pig.village.api.dto.FamilyLeaderDTO;
import com.pig4cloud.pig.village.api.dto.FamilySelect;
import com.pig4cloud.pig.village.api.dto.TeamSelect;
import com.pig4cloud.pig.village.api.dto.VillagerSelect;
import com.pig4cloud.pig.village.api.entity.Fund;
import com.pig4cloud.pig.village.api.entity.Welfare;
import com.pig4cloud.pig.village.api.request.RewardAdd;
import com.pig4cloud.pig.village.api.request.RewardNoFiles;
import com.pig4cloud.pig.village.api.request.RewardWithBase64File;
import com.pig4cloud.pig.village.api.vo.BASE64DecodedMultipartFile;
import com.pig4cloud.pig.village.api.vo.Base64File;
import com.pig4cloud.pig.village.appcollect.service.IAppCollectService;
import com.pig4cloud.pig.village.family.service.IFamilyService;
import com.pig4cloud.pig.village.fund.service.IFundService;
import com.pig4cloud.pig.village.reward.service.IRewardService;
import com.pig4cloud.pig.village.team.service.ITeamService;
import com.pig4cloud.pig.village.villager.service.IVillagerService;
import com.pig4cloud.pig.village.welfare.service.IWelfareService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * app汇总查询的controller
 *
 * @author limbray.li
 * @date 2021/7/3
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app-collect")
@Api(value = "appCollect", tags = "app统计汇总")
public class AppCollectController {
	@Resource
	private IRewardService rewardService;
	@Resource
	private IAppCollectService appCollectService;
	@Resource
	private ITeamService teamService;
	@Resource
	private IFamilyService familyService;
	@Resource
	private IVillagerService villagerService;
	@Resource
	private IWelfareService welfareService;
	@Resource
	private IFundService fundService;

	@ApiOperation(value = "首页")
	@GetMapping("/main-page")
	public R<AppMainDto> mainPage() {
		return R.ok(appCollectService.getMainPageData());
	}

	@ApiOperation(value = "五福基金")
	@GetMapping("/fund-page")
	public R<AppCollectDto> fundPage() {
		return R.ok(appCollectService.getFundPageData());
	}

	@ApiOperation(value = "自治事件")
	@GetMapping("/event-page")
	public R<AppEventDto> eventPage(@RequestParam("year") Integer year, @RequestParam("punishment") Integer punishment) {
		return R.ok(appCollectService.getEventPageData(year, punishment));
	}

	@ApiOperation(value = "提交")
	@PostMapping(value = "/add", headers = "content-type=multipart/form-data")
	public R add(RewardNoFiles reward
			, @ApiParam(value = "文件", required = false) @RequestPart("sign") MultipartFile sign
			, @ApiParam(value = "文件", required = false) @RequestPart("files") MultipartFile[] files) {
		RewardAdd rewardAdd = new RewardAdd();
		BeanUtil.copyProperties(reward, rewardAdd);
		rewardAdd.setSign(sign);
		rewardAdd.setFiles(files);
		return R.ok(rewardService.addReward(rewardAdd));
	}

	@ApiOperation(value = "提交")
	@PostMapping(value = "/addWithBase64")
	public R addWithBase64(@RequestBody RewardWithBase64File reward) {
		RewardAdd rewardAdd = new RewardAdd();
		BeanUtil.copyProperties(reward, rewardAdd);
		rewardAdd.setSign(BASE64DecodedMultipartFile.base64ToMultipart(reward.getBase64Sign()));
		MultipartFile[] multipartFiles = reward.getBase64Files().stream().map(e -> BASE64DecodedMultipartFile.base64ToMultipart(e)).toArray(MultipartFile[]::new);
		rewardAdd.setFiles(multipartFiles);
		return R.ok(rewardService.addReward(rewardAdd));
	}

	@ApiOperation(value = "查询全部组-all")
	@GetMapping("/listTeamAllSelect")
	public R<List<TeamSelect>> listTeamAllSelect() {
		return R.ok(teamService.listAllSelect());
	}

	@ApiOperation(value = "查询全部家庭-all")
	@GetMapping("/listFamilyAllSelect")
	public R<List<FamilySelect>> listFamilyAllSelect() {
		return R.ok(familyService.listAllSelect());
	}

	@ApiOperation(value = "查询全部家庭-by 组id")
	@GetMapping("/listFamilyByTeamIdSelect")
	public R<List<FamilySelect>> listFamilyByTeamIdSelect(Long teamId) {
		return R.ok(familyService.listByTeamIdSelect(teamId));
	}

	@ApiOperation(value = "全部村民下拉框-all")
	@GetMapping("/listVillagerAllSelect")
	public R<List<VillagerSelect>> listVillagerAllSelect() {
		return R.ok(villagerService.listAllSelect());
	}

	@ApiOperation(value = "全部村民下拉框-by 户id")
	@GetMapping("/listVillagerByFamilyIdSelect")
	public R<List<VillagerSelect>> listByFamilyIdSelect(Long familyId) {
		return R.ok(villagerService.listByFamilyIdSelect(familyId));
	}

	@ApiOperation(value = "全部村民下拉框-by 组id")
	@GetMapping("/listVillagerByTeamIdSelect")
	public R<List<VillagerSelect>> listByTeamIdSelect(Long teamId) {
		return R.ok(villagerService.listByTeamIdSelect(teamId));
	}

	@GetMapping("/listWelfare")
	@ApiOperation(value = "福利信息-自治金事件类型")
	public R<List<Welfare>> listWelfare() {
		return R.ok(welfareService.listAutomousWelfare());
	}

	@GetMapping("/listByFundId")
	@ApiOperation(value = "福利信息-by 基金id")
	public R<List<Welfare>> list(Long id) {
		return R.ok(welfareService.listByFundId(id));
	}

	@GetMapping("/listAllFund")
	@ApiOperation(value = "所有基金")
	public R<List<Fund>> listAllFund() {
		return R.ok(fundService.list());
	}

	@GetMapping("/listAllFamilyLeader")
	@ApiOperation(value = "所有户主信息")
	public R<List<FamilyLeaderDTO>> listAllFamilyLeader(){return R.ok(villagerService.listAllFamilyLeader());}
}
